package com.foreknow.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncoding implements Filter {
    private String encoding = "ISO8859-1";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //获取局部的初始化参数
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        //服务器端向客户端响应的内容的类型（MIME）    https://www.w3school.com.cn/media/media_mimeref.asp
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的字符编码集
        req.setCharacterEncoding("utf-8");
        //如果还有下一个过滤器，那么交给下一个过滤器来处理，如果没其他的过滤器，那么就交给Servlet来处理请求
        filterChain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
